package cn.com.cetccst.sdk.channel;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import cn.com.cetccst.sdk.modelbase.BaseResp;

public class SSAct {

    public static final String TAG = SSAct.class.getSimpleName();
    public static final String SS_APP_PACKAGE = "ss_appPackage";
    public static final String SS_APP_KEY = "ss_appKey";
    public static final String SS_SDK_VERSION = "ss_sdkVersion";
    public static final String SS_ACTIVITY_SUFFIX = ".ssapi.EntryActivity";
    public static final String PERMISSION_REGISTER_SUFFIX = ".permission.register";
    public static final String SSAPP_APP_KEY = "SS_APPKEY_V1_20250701_CETCCST";
    public static final String SS_TARGET_ACTIVITY_ENTRY = "com.cetccst.hostsdk.EntryActivity";

    public static final String SS_PACKAGE_NAME = "com.cetccst.securitysuite";

    public static String PERMISSION_REGISTER;
    public SSAct() {
    }

    public static int send(Context context, Args args, boolean multi) {
        if (context != null && args != null) {
            if (TextUtils.isEmpty(args.mTargetPkgName)) {
                return BaseResp.ErrCode.ERR_SDK_ERROR;
            } else {
                if (TextUtils.isEmpty(args.mTargetClassName)) {
                    args.mTargetClassName = args.mTargetPkgName + getComponentSuffix();
                } else if (!args.mTargetClassName.equals(SS_TARGET_ACTIVITY_ENTRY)) {
                    args.mTargetClassName = args.mTargetClassName + getComponentSuffix();
                }

                Intent intent = new Intent();
                intent.setClassName(args.mTargetPkgName, args.mTargetClassName);
                if (args.mBundle != null) {
                    intent.putExtras(args.mBundle);
                }

                String packageName = context.getPackageName();
                intent.putExtra(SS_APP_PACKAGE, packageName);
                if (!TextUtils.isEmpty(args.appKey)) {
                    intent.putExtra(SS_APP_KEY, args.appKey);
                }

                intent.putExtra(SS_SDK_VERSION, getSdkVersion(context, packageName));
                if (args.mFlags == -1) {
                    if (multi) {
                        intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                    }
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                } else {
                    intent.setFlags(args.mFlags);
                }

                try {
                    context.startActivity(intent);
                } catch (Exception exception) {
                    Log.e(TAG, "send fail, ex = " + exception.getMessage());
                    return BaseResp.ErrCode.ERR_SDK_ERROR;
                }

                return BaseResp.ErrCode.ERR_OK;
            }
        } else {
            Log.e(TAG, "send fail, invalid argument");
            return BaseResp.ErrCode.ERR_SDK_ERROR;
        }
    }

    private static String getComponentSuffix() {
        return SS_ACTIVITY_SUFFIX;
    }

    public static boolean sendBR(Context context, String action, Args args, String permission) {
        if (context != null && args != null) {
            Intent intent = new Intent();
            if (args.mBundle != null) {
                intent.putExtras(args.mBundle);
            }

            String packageName = context.getPackageName();
            intent.putExtra(SS_APP_PACKAGE, packageName);
            if (!TextUtils.isEmpty(args.appKey)) {
                intent.putExtra(SS_APP_KEY, args.appKey);
            }
            intent.putExtra(SS_SDK_VERSION, getSdkVersion(context, packageName));
            intent.setAction(action);
            try {
                if (TextUtils.isEmpty(permission)) {
                    context.sendOrderedBroadcast(intent, null);
                } else {
                    context.sendOrderedBroadcast(intent, permission);
                }
            } catch (Exception exception) {
                Log.e(TAG, "send fail, ex = " + exception.getMessage());
                return false;
            }
            return true;
        } else {
            Log.e(TAG, "send fail, invalid argument");
            return false;
        }
    }

    public static int getSdkVersion(Context context, String packageName) {
        ApplicationInfo applicationInfo = null;
        try {
            applicationInfo = context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA);
            return applicationInfo.metaData.getInt("com.raycom.securesms.OPEN_SDK_VERSION");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;

    }

    public static String getSuitePackage(Context context) {
        ApplicationInfo applicationInfo = null;
        try {
            applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            if (!TextUtils.isEmpty(applicationInfo.metaData.getString("SS_TARGET_PACKAGE_NAME"))) {
                return applicationInfo.metaData.getString("SS_TARGET_PACKAGE_NAME");
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return SS_PACKAGE_NAME;

    }

    public static class Args {
        public static final int INVALID_FLAGS = -1;
        public String mTargetPkgName;
        public String mTargetClassName;
        public String appKey;
        public int mFlags = -1;
        public Bundle mBundle;

        public String toString() {
            return "targetPkgName:" + mTargetPkgName + ", targetClassName:" + mTargetClassName + ", content:" + appKey + ", flags:" + mFlags + ", bundle:" + this.mBundle;
        }
    }
}
